package 字符串计算;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

/**
 * Created by hest0 on 2017/7/3.
 */

public class 四则运算 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            System.out.print(calculate(sc.nextLine()));
        }
        sc.close();
    }

    private static int calculate(String strExp) {
        String exp = strExp.replaceAll("\\[", "(").replaceAll("\\]", ")").replaceAll("\\{}", "(").replaceAll("\\}", ")");
        ArrayList<String> strList = getStringList(exp);//string转换为list
        return calculate(getPostOrder(strList));//计算
    }

    private static ArrayList<String> getStringList(String str) {
        ArrayList<String> result = new ArrayList<>();
        StringBuilder num = new StringBuilder();
        char pre = '(';
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isDigit(c) || c == '-') {
                if (c == '-' && (Character.isDigit(pre) || pre == ')')) {//判断是减号
                    if (num.length() != 0) {
                        result.add(num.toString());
                        num.setLength(0);
                    }
                    result.add(str.charAt(i) + "");
                } else//普通数字或者数字的符号
                    num.append(c);
            } else {
                if (num.length() != 0) {
                    result.add(num.toString());
                    num.setLength(0);
                }
                result.add(str.charAt(i) + "");
            }
            pre = c;
        }
        if (num.length() != 0)//对最后一个数做处理
        {
            result.add(num.toString());
            num.setLength(0);
        }
        return result;
    }

    //比较运算符
    private static boolean compare(String peek, String cur) {
        if ("*".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur)))
            return true;
        else if ("/".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur)))
            return true;
        else if ("+".equals(peek) && ("+".equals(cur) || "-".equals(cur)))
            return true;
        else if ("-".equals(peek) && ("+".equals(cur) || "-".equals(cur)))
            return true;
        return false;
    }

    //将中缀表达式转为后缀表达式
    private static ArrayList<String> getPostOrder(ArrayList<String> inOrderList) {
        ArrayList<String> result = new ArrayList<>();
        Stack<String> stack = new Stack<>();
        for (String s : inOrderList) {
            if (Character.isDigit(s.charAt(0)) || (s.length() > 1 && s.charAt(0) == '-')) {
                result.add(s);
            } else {
                switch (s.charAt(0)) {
                    case '(':
                        stack.push(s);
                        break;
                    case ')':
                        //peek 查看栈顶元素
                        while (!stack.peek().equals("(")) {
                            result.add(stack.pop());
                        }
                        stack.pop();
                        break;
                    default:
                        while (!stack.isEmpty() && compare(stack.peek(), s)) {
                            result.add(stack.pop());
                        }
                        stack.push(s);
                        break;
                }
            }
        }
        while (!stack.isEmpty()) {
            result.add(stack.pop());
        }
        return result;
    }

    //计算后缀表达式
    private static Integer calculate(ArrayList<String> postOrder) {
        Stack<Integer> stack = new Stack<>();
        for (String s : postOrder) {
            if (Character.isDigit(s.charAt(0)) || (s.length() > 1 && s.charAt(0) == '-')) {
                stack.push(Integer.parseInt(s));
            } else {
                Integer back = stack.pop();
                Integer front = stack.pop();
                Integer res = 0;
                switch (s.charAt(0)) {
                    case '+':
                        res = front + back;
                        break;
                    case '-':
                        res = front - back;
                        break;
                    case '*':
                        res = front * back;
                        break;
                    case '/':
                        res = front / back;
                        break;
                }
                stack.push(res);
            }
        }
        return stack.pop();
    }
}
